/*

pseudo code: 
- take max positive across simulatenous test
- then look for pos/neg

*/

use health/test_data_all, clear 

drop if lab_results == "Unknown" | lab_results == "Test Not Performed" ///
	| lab_result == "Inconclusive"

gen time = substr(lab_date, -8, 5)
replace time = subinstr(time, ":", "", .)
sort sid date time lab_results

by sid date time: gen tagree = lab_results == lab_results[1]
by sid date time: egen agree = min(tagree)
by sid date time: gen n_simul = _N
by sid date time: gen tag_time = _n==1

count if tag_time
count if tag_time & n_simul > 1
count if tag_time & ~agree

** keep positive case if simultaneous and positive present
assert positive | negative
sort sid date time negative 
by sid date time: keep if _n==1



foreach lead of numlist 1 3 7 14 {

	by sid: gen index_test = date - date[_n-1] > `lead'
	by sid: assert index_test[1] 
	by sid: gen retest = date[_n+1] - date <= `lead' & index_test

	gen type = 0 if index_test 
	by sid: replace type = 1 if positive & positive[_n+1]  & retest 
	by sid: replace type = 2 if positive & negative[_n+1]  & retest 
	by sid: replace type = 3 if negative & positive[_n+1]  & retest 
	by sid: replace type = 4 if negative & negative[_n+1]  & retest 
	
	gen pn = type == 2 & ~missing(type) & type ~= 0 
	gen np = type == 3 & ~missing(type) & type ~= 0 
	
	drop index_test retest
	rename (type np pn) (type`lead' np`lead' pn`lead')
}

label define type 0 "0. No retest" 1 "1. Pos-Pos" 2 "2. Pos-Neg" ///
	3 "3. Neg-Pos" 4 "4. Neg-Neg"

label values type1 type3 type7 type14 type

tab type1 
tab type1 if type1 ~= 0
tab type3 
tab type7 
tab type14 


foreach l of numlist 1 3 {
	sum 1.type`l' if type`l' ~=0 
	local p11 = r(mean)
	
	sum 2.type`l' if type`l' ~= 0
	local p10 = r(mean)
	
	sum 3.type`l' if type`l' ~= 0
	local p01 = r(mean)
	
	sum 4.type`l' if type`l' ~= 0 
	local p00 = r(mean)

	local p = (-1 + `p00' - `p11')^2/ (4*`p11')
	local alpha = (2*`p11')/(1-`p00'+`p11')
	noi di "p: `p'"
	noi di "alpha `alpha'"
	local npv = (1-`p')/(`p'*(1-`alpha') + (1-`p') )
	di `npv'
	
	count if type`l' ~= 0 & ~missing(type`l')
	local n = r(N)
	
	ttest pn`l' == np`l' if type`l' ~= 0 & ~missing(type`l')
	local pdiff = r(p)
	foreach stat in n p11 p00 p alpha npv p10 p01 pdiff {
		local replace ""
		if `l' == 1  & "`stat'" == "n" local replace "replace"
		
		scalarout using "tabless/test_retest_rates.csv", ///
			id("`stat' lead `l'") num(``stat'') fmt(%12.4f) `replace'
		
	}
}

